<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-table-cell {
            padding: 0px 10px;
        }
        .layui-table td, .layui-table th {
            min-width: 40px;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">人力资源管理</a>
            <a>
                <cite>人员列表</cite></a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form livingList">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">项目</label>
                                <div class="layui-input-block">
                                    <select name="city" lay-verify="required" id="project">
                                        <option value="null">-</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">工种</label>
                                <div class="layui-input-block" style="width:150px">
                                    <select name="city" lay-verify="required" id="peopleType">
                                        <option value="null">-</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">年度</label>
                                <div class="layui-input-block" style="width:150px">
                                    <select name="city" lay-verify="required" id="year">
                                        <option value="null">-</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-block" style="width:80px">
                                    <select name="city" lay-verify="required" id="status">
                                        <option value="null">-</option>
                                        <option value="1">在岗</option>
                                        <option value="3">请假</option>
                                        <option value="4">离职</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">搜索</label>
                                <div class="layui-input-block">
                                    <input type="search" autocomplete="off" placeholder="输入工人姓名/工号" class="layui-input" id="searchInput">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type='text/html' id='toolbar'>
    <button class="layui-btn add" type="button" auth="addEmployee">
        添加
    </button>
</script>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate","upload"], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
        $('#project').empty().html(projectSelect());
        $('#peopleType').empty().html(professionSelect());
        $('#year').empty().html(yearSelect());
        form.render();
        // 异步数据加载
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '人员列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/hrm/employee/lists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                projectId: 0,
                professionId: null,
                status: null,
                yearId: null,
                search: "",
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter',{title:'导入',layEvent:'upload',icon:'layui-icon-upload-circle'},{title:'导出',layEvent:'excel',icon:'layui-icon-export'}],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '50', fixed: 'left'},
                    {field: 'projectName', title: '项目', width: '160'},
                    {field: 'jobNumber', title: '工号', width: '5%'},
                    {field: 'name', title: '姓名', width: '6%'},
                    {
                        field: 'gender', title: '性别', width: '50', templet: function (d) {
                            return `${d.gender == 0 ? "-" : d.gender == 1 ? "男" : "女"}`
                        }
                    },
                    {field: 'age', title: '年龄', width: '50'},
                    {field: 'idcard', title: '身份证号', width: '150'},
                    {field: 'phone', title: '电话', width: '100'},
                    {field: 'professionName', title: '工种', width: '90'},
                    { field: 'groupName', title: '班组',width:'8%'},
                    {
                        field: 'contractTime', title: '签订时间', width: '90', templet: function (d) {
                            return d.contractTime ? d.contractTime.substr(0,10) : '-'
                        }
                    },
                    {
                        field: 'eduTime', title: '教育时间', width: '90', templet: function (d) {
                            return d.eduTime ? d.eduTime.substr(0,10) : '-'
                        }
                    },
                    {
                        field: 'status', title: '在岗状态', width: '60', templet: function (d) {
                            return `${d.status == 1 ? "在岗" : d.status == 2 ? "待岗" : d.status == 3 ? "请假" : d.status == 4 ? "离职" : ""}`
                        }
                    },
                    {
                        field: '', title: '操作', fixed: 'right', width: "270", templet: function (d) {
                            if (d.status == 1) {
                                return `
                                    <button class='layui-btn details' data-id=${d.id} lay-event="details" auth='employeeInfo'>详情</button>
                                    <button class='layui-btn edit' data-id=${d.id} lay-event="edit" auth='editEmployee'>编辑</button>
                                    <button class='layui-btn jobStatus' data-id=${d.id} lay-event="jobStatus" auth='employeeStatus'>工作状态</button>
                                    <button class='layui-btn distribution_table' data-id=${d.id} lay-event="distribution_table" auth='transferEmployee'>分配</button>
                                `
                            } else {
                                return `
                                    <button class='layui-btn details' data-id=${d.id} lay-event="details" auth='employeeInfo'>详情</button>
                                    <button class='layui-btn edit' data-id=${d.id} lay-event="edit" auth='editEmployee'>编辑</button>
                                    <button class='layui-btn jobStatus' data-id=${d.id} lay-event="jobStatus" auth='employeeStatus'>工作状态</button>
                                `
                            }
                        }
                    }
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done:function(){
                permission();
            }
        });

        table.on('tool(test)', function (data) {
            if (data.event == 'details') {
                var id = $(this).data('id');
                window.location.href = 'personnel_xq.html?id='+id;
            } else if (data.event == 'edit') {
                var id = $(this).data('id');
                localStorage.setItem('personnelList_detail-id', id);
                window.location.href = 'personnel_edit.html';
            } else if (data.event == 'jobStatus') {
                var ids = [data.data.id];
                layer.open({
                    type: 1,
                    title: "工作状态",
                    area: ['40%', '30%'],
                    content: `
                        <form action="" class="layui-form layerOpen" id="batchChangeStatus">
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">工作状态</label>
                                    <div class="layui-input-block">
                                        <select name="" id="status_z">
                                            <option value="0">请选择工作状态</option>
                                            <option value="1">在岗</option>
                                            <option value="2">待岗</option>
                                            <option value="4">离职</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </form>
                    `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        form.render();
                    },
                    yes: function (index, layero) {
                        var status = Number($('#batchChangeStatus select option:selected').val());
                        $.ajax({
                            url: url + '/hrm/employee/batchChangeStatus',
                            type: "post",
                            data: {
                                ids: ids,
                                status: status == 0 ? null : status,
                                token: $.parseJSON(localStorage.getItem('login_msg')).token
                            },
                            dataType: "json",
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('工作状态', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('工作状态', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'distribution_table') {
                var ids = [data.data.id];
                var list = projectSelect();
                layer.open({
                    type: 1,
                    title: "分配",
                    area: ['40%', '50%'],
                    content: `
                        <form action="" class="layui-form layerOpen" id="batchChangeProject">
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">项目</label>
                                    <div class="layui-input-block">
                                        <select name="" id="project">
                                            <option value="null">请选择项目</option>
                                            ${list}
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </form>
                    `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        form.render();
                    },
                    yes: function (index, layero) {
                        $.ajax({
                            url: url + '/hrm/employee/batchChangeProject',
                            type: "post",
                            traditional: true,
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify({
                                ids: ids,
                                projectId: Number($('#batchChangeProject select option:selected').val()),
                                token: getToken()
                            }),
                            dataType: "json",
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('调动项目', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('调动项目', r);
                                }
                            }
                        })
                    }
                })
            }
        });

        table.on('toolbar(test)', function (data) {
            if (data.event == 'excel') {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.open({
                        type: 0,
                        title: "工人列表",
                        area: ["40%", '30%'],
                        content: `当前年度工人列表记录正在生成中,请稍等!`,
                        btn: ['关闭'],
                        skin: 'my-skin'
                    })
                });
                $.ajax({
                    url: url + '/excel/export/employeeLists',
                    type: 'post',
                    data: {
                        projectId: $('#project option:selected').val() ? Number($('#project option:selected').val()) : 0,
                        professionId: $('#peopleType option:selected').val() == 'null' ? null : Number($('#peopleType option:selected').val()),
                        status: $('#status option:selected').val() == 'null' ? null : Number($('#status option:selected').val()),
                        token: $.parseJSON(localStorage.getItem('login_msg')).token
                    },
                    success: function (r) {
                        if (r.code == 100000) {
                            layui.use('layer', function () {
                                var layer = layui.layer;
                                layer.closeAll();
                                layer.open({
                                    type: 0,
                                    title: "导出",
                                    area: ["40%", '30%'],
                                    content: `<p class='one_p'>${r.msg}</p>`,
                                    btn: ['关闭'],
                                    skin: 'my-skin'
                                })
                            });
                        } else {
                            layui.use('layer', function () {
                                var layer = layui.layer;
                                layer.closeAll();
                                layer.open({
                                    type: 0,
                                    title: "导出",
                                    area: ["40%", '30%'],
                                    content: `<p class='one_p'>${r.msg}</p>`,
                                    btn: ['关闭'],
                                    skin: 'my-skin'
                                })
                            });
                        }
                    }
                })
            }else if(data.event == 'upload'){
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.open({
                        type: 0,
                        title: "工人导入",
                        area: ["40%", '30%'],
                        content: `点击按钮选择上传文件&nbsp;&nbsp;&nbsp;<button type="button" class="layui-btn layui-btn-xs" id="upload"><i class="layui-icon layui-icon-file"></i>上传文件</button>`,
                        btn: ['关闭'],
                        skin: 'my-skin',
                        success:function(){
                            var uploadInst = upload.render({
                                elem: '#upload' //绑定元素
                                ,accept:'file'
                                ,exts:'xls|xlsx'
                                ,data:{token: getParam('token')}
                                ,url: url+'/excel/import/employeeLists' //上传接口
                                ,done: function(r){
                                    if (r.code == 100000) {
                                        tips('工人导入', r);
                                        setTimeout(function () {
                                            layer.closeAll();
                                            table.reload('lists');
                                        }, 2000)
                                    } else {
                                        tips('工人导入', r);
                                    }
                                    //上传完毕回调
                                }
                                ,error: function(){
                                    //请求异常回调
                                }
                            });
                        },
                    })
                });
            }
        });
        //搜索
        $('body').on('click', 'button#search', function () {
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    projectId: $('#project option:selected').val() ? Number($('#project option:selected').val()) : 0,
                    professionId: $('#peopleType option:selected').val() == 'null' ? null : Number($('#peopleType option:selected').val()),
                    yearId: $('#year option:selected').val() == 'null' ? null : Number($('#year option:selected').val()),
                    status: $('#status option:selected').val() == 'null' ? null : Number($('#status option:selected').val()),
                    search: $('input#searchInput').val()
                }
            })
        })
        $('body').on('click', '.add', function () {
            window.location.href = './personnel_add.html'
        })
    });
</script>

</html>